package com.mashibing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mashibing.internalcommon.constant.CommonStatusEnum;
import com.mashibing.internalcommon.constant.DriverCarConstants;
import com.mashibing.internalcommon.dto.Car;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.OrderDriverResponse;
import com.mashibing.internalcommon.response.TerminalResponse;
import com.mashibing.internalcommon.response.TrackResponse;
import com.mashibing.mapper.CarMapper;
import com.mashibing.internalcommon.remote.ServiceMapClient;
import com.mashibing.service.ICarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 孟瑞达
 * @since 2025-06-18
 */
@Service
@Slf4j
public class CarServiceImpl implements ICarService {

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private ServiceMapClient serviceMapClient;

    @Override
    @Transactional
    public ResponseResult<Void> addCar(Car car) {

        // 判断车辆是否已存在
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address", car.getAddress());
        queryWrapper.eq("vehicle_no", car.getVehicleNo());
        Integer count = carMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.fail(CommonStatusEnum.CAR_ALREADY_EXIST.getCode(), CommonStatusEnum.CAR_ALREADY_EXIST.getMessage(), null);
        }

        LocalDateTime now = LocalDateTime.now();
        car.setGmtModified(now);
        car.setGmtCreate(now);
        carMapper.insert(car);

        // 获取tid
        ResponseResult<TerminalResponse> result = serviceMapClient.addTerminal(car.getVehicleNo(), car.getId().toString());
        String tid = result.getData().getTid();
        car.setTid(tid);

        // 获取trid
        ResponseResult<TrackResponse> tridResult = serviceMapClient.addTrack(tid);
        String trid = tridResult.getData().getTrid();
        car.setTrid(trid);

        // 保存车辆
        carMapper.updateById(car);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<Car> getDriverById(String carId) {
        Car car = carMapper.selectById(carId);
        log.info("carId:{}对应car信息:{}", carId, car);
        return ResponseResult.success(car);
    }

    @Override
    public ResponseResult<OrderDriverResponse> getDriverUserInfoByCarId(String carId) {
        // 查询与车辆绑定的司机的状态以及信息
        OrderDriverResponse orderDriverResponse = carMapper.selectDriverStateByCarId(carId, DriverCarConstants.DRIVER_CAR_BIND);

        // 查询car表中字段信息
        Car car = carMapper.selectById(carId);
        orderDriverResponse.setVehicleNo(car.getVehicleNo());

        return ResponseResult.success(orderDriverResponse);
    }


}
